﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DBUnitility;
using Models;
using BLL;
namespace AGVControlSystemPro
{
    
    public partial class FrmLogQuery : Form
    {
        private SqlDataPager objPager = null;
        public FrmLogQuery()
        {
            InitializeComponent();
            //初始化数据分页对象
            objPager = new SqlDataPager()
            {
                PrimaryKey = "LogId",//表的主键
                TableName = "LoginLogs",
                FiledName = "LogId,LoginId,SPName,ServerName,LoginTime,ExitTime",
                InnerName = "",
                CurrentPage = 1,
                Sort = "LoginTime ASC"//排序条件
            };
            //设置默认的显示条数
            this.cboRecordList.SelectedIndex = 1;
            this.dgvLogs.AutoGenerateColumns = false;
            //禁用相关按钮
            this.btnFirst.Enabled = false;
            this.btnNext.Enabled = false;
            this.btnPre.Enabled = false;
            this.btnLast.Enabled = false;
            this.btnToPage.Enabled = false;
        }
        //执行查询的公共方法
        private void Query()
        {
            //开启所有按钮
            this.btnFirst.Enabled = true;
            this.btnNext.Enabled = true;
            this.btnPre.Enabled = true;
            this.btnLast.Enabled = true;
            this.btnToPage.Enabled = true;

            //【1】设置分页查询的条件
            string sql = string.Format("LoginTime between '{0}' and '{1}'", this.dtpStart.Value.ToString("yyyy - MM - dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"));
            
            objPager.Condition = sql;
            //【2】设置每页显示的条数
            objPager.PageSize = Convert.ToInt32(this.cboRecordList.Text.Trim());
            //【3】执行查询
            this.dgvLogs.DataSource = objPager.GetPagedData();
            //【4】显示记录总数、显示总页数、显示当前页码
            this.lblRecordsCount.Text = objPager.RecordCount.ToString();
            this.lblPageCount.Text = objPager.TotalPages.ToString();
            if (this.lblPageCount.Text == "0")
            {
                this.lblCurrentPage.Text = "0";
            }
            else
            {
                this.lblCurrentPage.Text = objPager.CurrentPage.ToString();
            }

            //禁用按钮的情况
            if (this.lblPageCount.Text == "0" || this.lblPageCount.Text == "1")
            {
                this.btnFirst.Enabled = false;
                this.btnNext.Enabled = false;
                this.btnPre.Enabled = false;
                this.btnLast.Enabled = false;
                this.btnToPage.Enabled = false;
            }
            else
            {
                this.btnToPage.Enabled = true;
            }
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            objPager.CurrentPage = 1;//每次执行查询都要设置为第1页
            Query();
            this.btnPre.Enabled = false;
            this.btnFirst.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnToPage_Click(object sender, EventArgs e)
        {
            if (this.txtToPage.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入要跳转的页码！", "信息提示");
                this.txtToPage.Focus();
                return;
            }
            //使用正则表达式验证必须为大于0的正整数...
            if (!Common.DataValidate.IsPositiveInteger(this.txtToPage.Text.Trim()))
            {
                MessageBox.Show("跳转页码必须为为正整数","信息提示");
                this.txtToPage.Focus();
                return;
            }
            int toPage = Convert.ToInt32(this.txtToPage.Text.Trim());
            if (toPage > objPager.TotalPages)
            {
                MessageBox.Show("跳转的页数不能大于数据总页数！", "信息提示");
                this.txtToPage.Focus();
                this.txtToPage.SelectAll();
                return;
            }
            //开始跳转页数
            objPager.CurrentPage = toPage;
            Query();
            if (objPager.CurrentPage == 1)
            {
                this.btnPre.Enabled = false;
                this.btnFirst.Enabled = false;
            }
            else if (objPager.CurrentPage == objPager.TotalPages)
            {
                this.btnNext.Enabled = false;
                this.btnLast.Enabled = false;
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            btnQuery_Click(null, null);
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            objPager.CurrentPage -= 1;
            Query();
            if (objPager.CurrentPage == 1)
            {
                this.btnPre.Enabled = false;
                this.btnFirst.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            objPager.CurrentPage += 1;
            Query();
            //当执行到最后一页的时候应该禁用最后一页和下一页的按钮
            if (objPager.CurrentPage == objPager.TotalPages)
            {
                this.btnNext.Enabled = false;
                this.btnLast.Enabled = false;
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            objPager.CurrentPage = objPager.TotalPages;
            Query();
            this.btnNext.Enabled = false;
            this.btnLast.Enabled = false;
        }

        private void dgvLogs_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvLogs, e);
        }

        private void FrmLogQuery_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmLogQuery = null;
        }
    }
}
